using IrisGenerator.Common.Tools;
using System;
namespace IrisGenerator.Common.DatabaseModel {
    
    public partial class Table {
        
        private System.Nullable<System.Guid> _tabId;
        
        private System.Nullable<System.Guid> _dbId;
        
        private string _tabName;
        
        private string _tabOwner;
        
        private string _tabFormatedName;
        
        private System.Nullable<bool> _tabIsView;
        
        private ColumnCollection _columnCollection;
        
        private RequestCollection _requestCollection;
        
        private Database _database;
        
        public virtual System.Nullable<System.Guid> TabId {
            get {
                return _tabId;
            }
            set {
                _tabId = value;
            }
        }
        
        public virtual string TabName {
            get {
                return _tabName;
            }
            set {
                _tabName = value;
            }
        }
        
        public virtual string TabOwner {
            get {
                return _tabOwner;
            }
            set {
                _tabOwner = value;
            }
        }
        
        public virtual string TabFormatedName {
            get {
                return _tabFormatedName;
            }
            set {
                _tabFormatedName = value;
            }
        }
        
        public virtual System.Nullable<bool> TabIsView {
            get {
                return _tabIsView;
            }
            set {
                _tabIsView = value;
            }
        }
        
        public virtual ColumnCollection ColumnCollection {
            get {
                if ((this._columnCollection == null)) {
                    _columnCollection = IrisGenerator.Common.DatabaseModel.Column.Select_COLUMN_By_TAB_ID(this.TabId);
                }
                return this._columnCollection;
            }
        }
        
        public virtual RequestCollection RequestCollection {
            get {
                if ((this._requestCollection == null)) {
                    _requestCollection = IrisGenerator.Common.DatabaseModel.Request.Select_REQUEST_By_TAB_ID(this.TabId);
                }
                return this._requestCollection;
            }
        }
        
        public virtual Database Database {
            get {
                if ((this._database == null)) {
                    this._database = IrisGenerator.Common.DatabaseModel.Database.Load(this._dbId);
                }
                return this._database;
            }
            set {
                _database = value;
                if (value != null)
                    _dbId = value.DbId;
                else
                    _dbId = null;
            }
        }
        
        private void Clean() {
            this.TabId = null;
            this.TabName = string.Empty;
            this.TabOwner = string.Empty;
            this.TabFormatedName = string.Empty;
            this.TabIsView = null;
            this._columnCollection = null;
            this._requestCollection = null;
            this.Database = null;
        }
        
        private void Fill(System.Data.DataRow dr) {
            this.Clean();
            if ((dr["TAB_ID"] != System.DBNull.Value)) {
                this.TabId = ((System.Nullable<System.Guid>)(dr["TAB_ID"]));
            }
            if ((dr["DB_ID"] != System.DBNull.Value)) {
                this._dbId = ((System.Nullable<System.Guid>)(dr["DB_ID"]));
            }
            if ((dr["TAB_NAME"] != System.DBNull.Value)) {
                this.TabName = ((string)(dr["TAB_NAME"]));
            }
            if ((dr["TAB_OWNER"] != System.DBNull.Value)) {
                this.TabOwner = ((string)(dr["TAB_OWNER"]));
            }
            if ((dr["TAB_FORMATED_NAME"] != System.DBNull.Value)) {
                this.TabFormatedName = ((string)(dr["TAB_FORMATED_NAME"]));
            }
            if ((dr["TAB_IS_VIEW"] != System.DBNull.Value)) {
                this.TabIsView = ((System.Nullable<bool>)(dr["TAB_IS_VIEW"]));
            }
        }
        
        public static TableCollection Select_TABLE_By_DB_ID(System.Nullable<System.Guid> DB_ID) {
            IrisGenerator.Common.DatabaseModel.DataAccess.Table dbo = null;
            try {
                dbo = new IrisGenerator.Common.DatabaseModel.DataAccess.Table();
                System.Data.DataSet ds = dbo.Select_TABLE_By_DB_ID(DB_ID);
                TableCollection collection = new TableCollection();
                if (GlobalTools.IsSafeDataSet(ds)) {
                    for (int i = 0; (i < ds.Tables[0].Rows.Count); i = (i + 1)) {
                        Table obj = new Table();
                        obj.Fill(ds.Tables[0].Rows[i]);
                        if ((obj != null)) {
                            collection.Add(obj);
                        }
                    }
                }
                return collection;
            }
            catch (System.Exception ) {
                throw;
            }
            finally {
                if ((dbo != null)) {
                    dbo.Dispose();
                }
            }
        }
        
        public static TableCollection GetAll() {
            IrisGenerator.Common.DatabaseModel.DataAccess.Table dbo = null;
            try {
                dbo = new IrisGenerator.Common.DatabaseModel.DataAccess.Table();
                System.Data.DataSet ds = dbo.TABLE_Select_All();
                TableCollection collection = new TableCollection();
                if (GlobalTools.IsSafeDataSet(ds)) {
                    for (int i = 0; (i < ds.Tables[0].Rows.Count); i = (i + 1)) {
                        Table obj = new Table();
                        obj.Fill(ds.Tables[0].Rows[i]);
                        if ((obj != null)) {
                            collection.Add(obj);
                        }
                    }
                }
                return collection;
            }
            catch (System.Exception ) {
                throw;
            }
            finally {
                if ((dbo != null)) {
                    dbo.Dispose();
                }
            }
        }
        
        public static Table Load(System.Nullable<System.Guid> TAB_ID) {
            IrisGenerator.Common.DatabaseModel.DataAccess.Table dbo = null;
            try {
                dbo = new IrisGenerator.Common.DatabaseModel.DataAccess.Table();
                System.Data.DataSet ds = dbo.TABLE_Select_One(TAB_ID);
                Table obj = null;
                if (GlobalTools.IsSafeDataSet(ds)) {
                    if ((ds.Tables[0].Rows.Count > 0)) {
                        obj = new Table();
                        obj.Fill(ds.Tables[0].Rows[0]);
                    }
                }
                return obj;
            }
            catch (System.Exception ) {
                throw;
            }
            finally {
                if ((dbo != null)) {
                    dbo.Dispose();
                }
            }
        }
        
        public virtual void Load() {
            IrisGenerator.Common.DatabaseModel.DataAccess.Table dbo = null;
            try {
                dbo = new IrisGenerator.Common.DatabaseModel.DataAccess.Table();
                System.Data.DataSet ds = dbo.TABLE_Select_One(this.TabId);
                if (GlobalTools.IsSafeDataSet(ds)) {
                    if ((ds.Tables[0].Rows.Count > 0)) {
                        this.Fill(ds.Tables[0].Rows[0]);
                    }
                }
            }
            catch (System.Exception ) {
                throw;
            }
            finally {
                if ((dbo != null)) {
                    dbo.Dispose();
                }
            }
        }
        
        public virtual Guid? Insert() {
            IrisGenerator.Common.DatabaseModel.DataAccess.Table dbo = null;
            Guid? result = null;
            try {
                dbo = new IrisGenerator.Common.DatabaseModel.DataAccess.Table();
                result=dbo.TABLE_Insert(this._dbId, this.TabName, this.TabOwner, this.TabFormatedName, this.TabIsView);
                this.TabId = result;
                return result;
            }
            catch (System.Exception ) {
                throw;
            }
            finally {
                if ((dbo != null)) {
                    dbo.Dispose();
                }
            }
        }
        
        public virtual void Delete() {
            IrisGenerator.Common.DatabaseModel.DataAccess.Table dbo = null;
            try {
                dbo = new IrisGenerator.Common.DatabaseModel.DataAccess.Table();
                dbo.TABLE_Delete(this.TabId);
            }
            catch (System.Exception ) {
                throw;
            }
            finally {
                if ((dbo != null)) {
                    dbo.Dispose();
                }
            }
        }
        
        public virtual void Update() {
            IrisGenerator.Common.DatabaseModel.DataAccess.Table dbo = null;
            try {
                dbo = new IrisGenerator.Common.DatabaseModel.DataAccess.Table();
                dbo.TABLE_Update(this.TabId, this._dbId, this.TabName, this.TabOwner, this.TabFormatedName, this.TabIsView);
            }
            catch (System.Exception ) {
                throw;
            }
            finally {
                if ((dbo != null)) {
                    dbo.Dispose();
                }
            }
        }
    }

}
